﻿using CentaLine.Infrastructure;
using ETA.Entity;
using ETA.IRepository;
using ETA.IService;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ETA.Service
{
    public class WorkTypeInfoService : IWorkTypeInfoService
    {
        #region 属性

        private IWorkTypeInfoRepository _IWorkTypeInfoRepository { set; get; }

        #endregion

        #region 构造函数

        public WorkTypeInfoService(IWorkTypeInfoRepository IWorkTypeInfoRepository)
        {
            _IWorkTypeInfoRepository = IWorkTypeInfoRepository;
        }

        #endregion

        public QueryWorkTypeInfoResponse QueryWorkTypeInfoList(QueryWorkTypeInfoRequest req)
        {
            var key = "KeyWorkTypeInfoList";
            if (!string.IsNullOrEmpty(req.WorkTypeId))
            {
                key += req.WorkTypeId;
            }
            if (CacheHelper.Get(key) != null)
            {
                return CacheHelper.Get(key) as QueryWorkTypeInfoResponse;
            }

            var response = new QueryWorkTypeInfoResponse();
            try
            {
                var workTypeInfoList = new List<WorkTypeInfo>();
                if (!string.IsNullOrEmpty(req.WorkTypeId))
                {
                    workTypeInfoList = _IWorkTypeInfoRepository.GetAllList(b => b.WorkTypeId == req.WorkTypeId);
                    response.Result.IsSuccess = true;
                }
                else
                {
                    workTypeInfoList = _IWorkTypeInfoRepository.GetAllList();
                    response.Result.IsSuccess = true;
                }
                response.WorkTypeInfoList = workTypeInfoList;
                if (response.Result.IsSuccess)
                {
                    CacheHelper.AddHours(key, response, 20);
                }
            }
            catch (Exception e)
            {
                response.Result.ErrMessage = e.Message;
            }
            return response;
        }
    }
}
